Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Listing 19.1 Objects and properties in ADDRESS.FRM.
    Begin VB.Form frmAddress
       BorderStyle    =   1  ‘Fixed Single
       Caption        =   “Address Book”
       MaxButton      =   0   ‘False
       MinButton      =   0   ‘False
       Begin VB.CommandButton cmdAction
          Caption         =   “List”
          Index           =   2
       End
       Begin VB.CommandButton cmdAction
          Caption         =   “Exit”
          Index           =   3
       End
       Begin VB.CommandButton cmdAction
          Caption         =   “Delete”
          Index           =   1
       End
       Begin VB.CommandButton cmdAction
          Caption         =   “New”
          Index           =   0
       End
       Begin VB.CommandButton cmdMove
          Caption         =   “>>”
          Index           =   3
       End
       Begin VB.CommandButton cmdMove
          Caption         =   “>”
          Index           =   2
       End
       Begin VB.CommandButton cmdMove
          Caption         =   “<”
          Index           =   1
       End
       Begin VB.CommandButton cmdMove
          Caption         =   “<<”
          Index           =   0
       End
       Begin VB.TextBox txtZip
       End
       Begin VB.TextBox txtState
       End
       Begin VB.TextBox txtCity
       End
       Begin VB.TextBox txtAddress
       End
       Begin VB.TextBox txtFName
       End
       Begin VB.TextBox txtLName
       End
       Begin VB.Label Label6
          Caption         =   “ZIP:”
       End
       Begin VB.Label Label5
          Caption         =   “State:”
       End
       Begin VB.Label Label4
          Caption         =   “City:”
       End
       Begin VB.Label Label3
          Caption         =   “Address:”
       End
       Begin VB.Label Label2
          Caption         =   “First name:”
       End
       Begin VB.Label Label1
          Caption         =   “Last name:”
       End
    End
#### Adding A Basic Module Rather than placing all of the program code in the form module, we will use a Basic module to hold the Type definition and the global variable declarations. A Basic module contains no visual elements—only code. Why bother with a separate module? Why not just place all the code in the form module? The most important reason is that you can reuse Basic modules, which serve as software components. When writing procedures that have general utility, place them in a Basic module. To use those procedures in another Visual Basic project, simply add the Basic module to the project (using the Add File command on the Project menu). A less important, although still valid, reason is that a Basic module is the best place to declare variables and define types that are global to two or more modules. This is what we will do in the current project. Notice that the Basic module in this project contains no procedures, only variable declarations. Strictly speaking, we could have finished this project without a Basic module, but the preferred programming practice is to place global variable declarations in a Basic module rather than scattering them around the various form modules (we will be adding a second form to this project in a bit). Bottom line: You need to learn to use Basic modules, and now is as good a time as any. To add a Basic module to the project, pull down the Project menu and select Add Module. A Code Editing window will open with a default name in its title bar. Enter the code shown in Listing 19.2. After you have finished entering the code, select Save from the File menu and assign the name ADDRESS.BAS to the code module file. Listing 19.2 Code in ADDRESS.BAS.
    Option Explicit

    Type Address
        FName As String * 20
        LName As String * 20
        Address As String * 40
        City As String * 15
        State As String * 2
        Zip As String * 5
    End Type

    Public CR As Address
    Public EnteringNew As Boolean
    Public CurrentRecord As Integer
    Public NumberOfRecords As Integer
    Public OldRecord As Integer
    Public FullFileName As String
    Public FileNum As Integer
    Public Const FILENAME = “ADDRESS.DAT”
Look at the code in Listing 19.2. First, the Type statement defines the user-defined data structure, Address. This structure contains six fixed-length string elements to hold the six fields of the address record. Next are declarations of the program’s global variables. Note the use of the Public keyword in place of the usual Dim keyword. By using Public, you specify that the variable is global to all modules in the program, not just to the module where it is declared. If we had used Dim instead, the scope of these variables would be limited to the Basic module. The first variable (CR) is an instance of the Address type. The program will use CR to hold the current record. Don’t we need an array of type Address to hold the entire database? That’s one solution, but I’ve used a different method. The database itself will remain stored on disk. Only the current record—the one being viewed, modified, or added—will be read into memory. Changes to the database will immediately and automatically be written to disk. Note that we define a constant to hold the name of the database file. The program always works with the same database file, assuming that most people need only a single address list. We could modify the program to let the user specify the database file if that feature would be valuable. The Common Dialog control would simplify the task of opening and saving files, and you could add this feature to the program if desired. The other variables should be self-explanatory. You’ll see how they are applied in subsequent code listings. #### Using Multiple Forms In the address database’s design specification, we said that the program should be able to list all entries in the database in alphabetical order. Rather than trying to squeeze this list onto our existing form, we’ll use a separate form. The List Box control is ideal for displaying our list, because it has a Sorted property that causes items in the List Box to automatically sort alphabetically when it is set to True. To add a second form to the project, select Add Form from the Project menu. A new, blank form will be displayed. Place a List Box control on the form; the size and position do not matter, because they will be set in code when the form is displayed. Set the List Box’s Sorted property to True, and the form’s Caption property to Address List. Select Save File from the File menu and save the form as FRMLIST.FRM. The form’s objects and properties are given in Listing 19.3. Listing 19.3 Objects and properties in FRMLIST.FRM.
    Begin VB.Form frmList
       Caption       =   “Address List”
       Begin VB.ListBox List1
          Sorted          =   -1  ‘True
       End
    End
When a project contains more than one form, how does it know where to start when the program executes? Every multiple-form project has a startup form, which is displayed first when the program begins execution. The display of other forms is then controlled by program code. By default, the first form you create in a project is the startup form. To change the startup form, select Properties from the Project menu, then click on the General tab in the displayed dialog box. Pull down the Startup Object list and select the desired startup form. We don’t need to change the startup form for this project.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.